<?php
namespace app\restaurant\model;

use app\BaseModel;
use think\facade\Db;

class Delivery extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_store_delivery';


    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:05
     * @功能说明:配置详情
     */
    public function configInfo($dis){

        $data = $this->where($dis)->find();

//        if(empty($data)){
//
//            $this->configAdd($dis);
//
//            $data = $this->where($dis)->find();
//        }

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:配置添加
     */
    public function configAdd($data){

        $res = $this->insert($data);


        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:配置编辑
     */
    public function configUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-10 18:00
     * @功能说明:列表
     */
    public function configList($dis,$page = 10){

        $data = $this->where($dis)->order('status desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-12 14:40
     * @功能说明:获取待付款那里的
     */
    public function getFee($store_id,$address){

        $store_model   = new Store();
        //获取门店地址
        $store   = $store_model->storeInfo(['id'=>$store_id]);
        //计算距离
        $distance = getdistance($address['long'],$address['lat'],$store['long'],$store['latitude']);

        $dis = [

            'store_id' => $store_id,

            'status'   => 1
        ];

        $delivery = $this->configInfo($dis);

        if(empty($delivery)){

            return 0;

            return ['code'=>500,'msg'=>'平台未设置配送费'];
        }
        //计算超出配送距离
        $out_distance = ceil($distance/1000-$delivery['start_distance']);
        //计算超出配送距离对费用
        $out_fee = $out_distance>0?$out_distance*$delivery['km_delivery_fee']:0;
        //总对配送费
        $fee = round($delivery['delivery_fee']+$out_fee,2);

        return $fee;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-18 14:22
     * @功能说明:检测距离
     */
    public function checkDistance($store_id,$address_id){

        $store_model   = new Store();

        $config_model  = new StoreConfig();
        //获取门店地址
        $store   = $store_model->storeInfo(['id'=>$store_id]);

        $address = Db::name('longbing_card_shop_address')->where(['id'=>$address_id])->find();

        if(empty($address)){

            return ['code'=>500,'msg'=>'请选择配送地址'];
        }
        //计算距离
        $distance = getdistance($address['long'],$address['lat'],$store['long'],$store['latitude']);

        $config   = $config_model->configInfo(['store_id'=>$store_id,'uniacid'=>$address['uniacid']]);

        if($distance>$config['send_distance']*1000){

            return ['code'=>500,'msg'=>'超出配送范围'];

        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-12 14:13
     * @功能说明:获取配送费并返回一个拼接好的订单
     */

    public function getFeeOrder($order){

        $order_model   = new Order();

        $address_model = new OrderAddress();
        //获取下单地址
        $address = $address_model->dataInfo(['order_id'=>$order['id']]);

        $fee = $this->getFee($order['store_id'],$address);

        if(!empty($fee['code'])){

            return $fee;
        }

        $order_model->orderUpdate(['id'=>$order['id']],['express_price'=>$fee,'pay_price'=>$order['pay_price']+$fee]);

        $order = $order_model->orderInfo(['id'=>$order['id']]);

        return $order;

    }










}